package com.lenovo.tool;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Properties;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

class PropConfigHandler extends DefaultHandler {

    private Properties props;
    private ArrayList<String> excludePaths = new ArrayList<String>();
    private HashSet<String> excludeArrays = new HashSet<String>();

    private StringBuffer currentValue = new StringBuffer();

    public PropConfigHandler() {
        this.props = new Properties();
    }

    public Properties getProps() {
        return this.props;
    }

    public ArrayList<String> getExcludePaths() {
        return this.excludePaths;
    }

    public HashSet<String> getExcludeArrays() {
        return this.excludeArrays;
    }

    public void characters(char[] ch, int start, int length)
            throws SAXException {
        currentValue.append(ch, start, length);
    }

    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if ("exclude-path".equals(qName)) {
            excludePaths.add(currentValue.toString().trim());
            currentValue.delete(0, currentValue.length());
        } else if ("exclude-array".equals(qName)) {
            excludeArrays.add(currentValue.toString().trim());
            currentValue.delete(0, currentValue.length());
        } else if (!("config".equals(qName))) {
            props.put(qName.toLowerCase(), currentValue.toString().trim());
            currentValue.delete(0, currentValue.length());
        }
    }
}
